﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TraderClient.Models
{
    /// <summary>
    /// 市场类型
    /// </summary>
    public enum MarketType
    {
        /// <summary>
        /// 期货市场
        /// </summary>
        Futures,

        /// <summary>
        /// 股票市场
        /// </summary>
        Stock,

        /// <summary>
        /// 外汇市场
        /// </summary>
        Forex
    }

    /// <summary>
    /// 买卖方向
    /// </summary>
    public enum DirectionType
    {
        /// <summary>
        /// 多头
        /// </summary>
        Buy,

        /// <summary>
        /// 空头
        /// </summary>
        Sell
    }

    /// <summary>
    /// 表示行情最新价格的类
    /// </summary>
    public class LastPrice
    {
        /// <summary>
        /// 市场类型
        /// </summary>
        public MarketType Market { get; set; }

        /// <summary>
        /// 代码（股票代码，期货合约代码等）
        /// </summary>
        public string Code { get; set; }

        /// <summary>
        /// 最新价格
        /// </summary>
        public double Price { get; set; }

        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime UpdateTime { get; set; }
    }

    /// <summary>
    /// 持仓汇总类
    /// </summary>
    public class Position
    {
        /// <summary>
        /// 市场类型
        /// </summary>
        public MarketType Market { get; set; }

        /// <summary>
        /// 策略名称
        /// </summary>
        public string Policy { get; set; }

        /// <summary>
        /// 代码
        /// </summary>
        public string Code { get; set; }

        /// <summary>
        /// 买卖方向
        /// </summary>
        public DirectionType Direction { get; set; }

        /// <summary>
        /// 持仓总数量
        /// </summary>
        public int Volume 
        { 
            get 
            {
                int volume = 0;

                foreach (PositionDetail pd in Detail)
                {
                    volume += pd.Volume;
                }

                return volume;
            } 
        }

        /// <summary>
        /// 持仓总金额
        /// </summary>
        public double TotalAmount
        {
            get
            {
                double total = 0.0;

                foreach (PositionDetail pd in Detail)
                {
                    total += pd.OpenPrice * pd.Volume;
                }

                return total;
            }
        }

        /// <summary>
        /// 持仓平均成本
        /// </summary>
        public double AverageCost
        { 
            get 
            {
                return TotalAmount / Volume;
            } 
        }

        /// <summary>
        /// 持仓明细
        /// </summary>
        public List<PositionDetail> Detail { get; set; }
    }

    /// <summary>
    /// 持仓明细类
    /// </summary>
    public class PositionDetail
    {
        /// <summary>
        /// 账号
        /// </summary>
        public string Account { get; set; }

        /// <summary>
        /// 开仓时间
        /// </summary>
        public DateTime OpenTime { get; set; }

        /// <summary>
        /// 开仓价格
        /// </summary>
        public double OpenPrice { get; set; }

        /// <summary>
        /// 数量
        /// </summary>
        public int Volume { get; set; }
    }
}
